<?php
namespace Dry\Base;

class RequestBase {

    private $raw = array();

    public function __construct($raw)
    {
        $this->raw = $raw;
    }

    public function set($key, $value)
    {
        $this->raw[$key] = $value;
    }

    public function remove($key)
    {
        if(isset($this->raw[$key])){
            unset($this->raw[$key]);
        }
    }

    public function get($key, $default='')
    {
        if(isset($this->raw[$key])){
            return $this->raw[$key];
        }
        else{
            return $default;
        }
    }

    public function all()
    {
        return $this->raw;
    }

    public function keys()
    {
        return array_keys($this->raw);
    }

    public function has($key)
    {
        if(isset($this->raw[$key])){
            return true;
        }
        else{
            return false;
        }
    }

    public function count()
    {
        return count($this->raw);
    }

    public function getAlpha($key, $default='')
    {
        return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default));
    }

    public function getAlnum($key, $default='')
    {
        return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default));
    }

    public function getInt($key, $default=0)
    {
        return (int) $this->get($key, $default);
    }

    public function getSafe($key, $default='')
    {
        $string = $this->get($key, $default);
        return $this->replaceButSave($string, 'a-zA-Z0-9-_@');
    }

    private function replaceButSave($string, $char)
    {
        return preg_replace("/[^{$char}]/", '', $string);
    }

}
?>
